using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory.Support;
using Spring.Remoting;
using System;
using System.Diagnostics;
namespace AppToolKit.Core.Context.Spring.AutoConstruction.Builder
{
	public abstract class ClientBuilderBase : IBusinessBuilder, IApplicationContextAware
	{
		private ContextHelper helper_;
		private IApplicationContext context_;
		public IApplicationContext ApplicationContext
		{
			get
			{
				return this.context_;
			}
			set
			{
				this.context_ = value;
				this.helper_ = new ContextHelper(value);
			}
		}
		public string Uri
		{
			get;
			set;
		}
		public IBusinessManager BusinessManager
		{
			get;
			set;
		}
		public object GetObject(Type businessType)
		{
			string businessName = this.GetBusinessName(businessType);
			if (!this.helper_.ContainsObjectDefine(BusinessNameSymbol.GetReferenceName(businessName)))
			{
				Debug.Assert(this.BusinessManager != null);
				this.BusinessManager.RegisterBusiness(businessType);
                DefaultListableObjectFactory defaultListableObjectFactory = (DefaultListableObjectFactory)((XmlApplicationContext)this.ApplicationContext).ObjectFactory;
				RootObjectDefinition rootObjectDefinition = new RootObjectDefinition(typeof(SaoFactoryObject));
				rootObjectDefinition.IsSingleton=true;
				string interfaceName = this.GetInterfaceName(businessType);
				rootObjectDefinition.PropertyValues.Add("ServiceInterface", interfaceName);
				string text = this.Uri + BusinessNameSymbol.GetExposeName(businessName);
				rootObjectDefinition.PropertyValues.Add("ServiceUrl", text);
				defaultListableObjectFactory.RegisterObjectDefinition(BusinessNameSymbol.GetReferenceName(businessName), rootObjectDefinition);
			}
			return this.helper_.GetObject(BusinessNameSymbol.GetReferenceName(businessName));
		}
		public abstract string GetInterfaceName(Type businessType);
		public abstract bool IsSupport(Type type);
		protected abstract string GetBusinessName(Type businessType);
	}
}
